﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.ServiceLocation;
using AvalonDock;
using System.IO;
using System.ComponentModel.Composition;
using System.Xml.Linq;
using System.Xml;

namespace DynamicUIFramework
{
    /// <summary>
    /// Workbench.xaml 的交互逻辑
    /// </summary>
    [Export]
    public partial class Workbench : UserControl
    {
        public Workbench()
        {
            InitializeComponent();

            this.dm.DeserializationCallback += (s, e) =>
            {
                var dockableContent = new RegionContent(e.Name);
                e.Content = dockableContent;
            };
        }

        public List<MenuItem> GetMenu()
        {
            List<MenuItem> list = new List<MenuItem>();
            MenuItem mi = new MenuItem { Header = "Designer" };
            if (DesignerContext.IsInDesignMode)
            {
                MenuItem newContent = new MenuItem { Header = "NewContent" };
                MenuItem addContent = new MenuItem { Header = "AddContent" };
                MenuItem save = new MenuItem { Header = "Save" };
                MenuItem load = new MenuItem { Header = "Load" };
                bool isin = DesignerContext.IsInDesignMode;
                MenuItem miM = new MenuItem { Header = isin == true ? "OutDesigner" : "InDesigner", Tag = isin };
                mi.Items.Add(newContent);
                mi.Items.Add(addContent);
                mi.Items.Add(save);
                mi.Items.Add(load);
                mi.Items.Add(miM);

                list.Add(mi);

                #region action

                MenuItem canvas = new MenuItem { Header = "Canvas" };
                canvas.Click += (s, e) =>
                {
                    NameDialog d = new NameDialog();
                    bool? ret = d.ShowDialog();
                    if (ret == true)
                    {
                        if (string.IsNullOrEmpty(d.TargetName) || string.IsNullOrEmpty(d.TargetTitle))
                        {
                            MessageBox.Show("名字和标题不能为空！");
                            return;
                        }

                        foreach (var item in this.dm.DockableContents)
                        {
                            if (item.Name.Equals(d.TargetName))
                            {
                                MessageBox.Show("已经存在相同名字对象");
                                return;
                            }
                        }
                        RegionController rc = ServiceLocator.Current.GetInstance<RegionController>();
                        if (rc.Regions.ContainsKey(d.TargetName))
                        {
                            MessageBoxResult result = MessageBox.Show("在本地目录中已经存在相同名字对象", "添加", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
                            if (result == MessageBoxResult.No)
                                return;
                        }
                        RegionContent r = new RegionContent(d.TargetName);
                        r.Title = d.TargetTitle;
                        r.Content = new DesignerCanvas();
                        r.Background = Brushes.White;
                        (r.Content as Panel).Background = r.Background;
                        this.dm.MainDocumentPane.Items.Add(r);
                    }
                };
                newContent.Items.Add(canvas);

                MenuItem panel = new MenuItem { Header = "Panel" };
                panel.Click += (s, e) =>
                {
                    NameDialog d = new NameDialog();
                    bool? ret = d.ShowDialog();
                    if (ret == true)
                    {
                        if (string.IsNullOrEmpty(d.TargetName) || string.IsNullOrEmpty(d.TargetTitle))
                        {
                            MessageBox.Show("名字和标题不能为空！");
                            return;
                        }

                        foreach (var item in this.dm.DockableContents)
                        {
                            if (item.Name.Equals(d.TargetName))
                            {
                                MessageBox.Show("已经存在相同名字对象");
                                return;
                            }
                        }
                        RegionController rc = ServiceLocator.Current.GetInstance<RegionController>();
                        if (rc.Regions.ContainsKey(d.TargetName))
                        {
                            MessageBoxResult result = MessageBox.Show("在本地目录中已经存在相同名字对象", "添加", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
                            if (result == MessageBoxResult.No)
                                return;
                        }
                        RegionContent r = new RegionContent(d.TargetName);
                        r.Title = d.TargetTitle;
                        r.Content = new DesignerPanel();
                        r.Background = Brushes.White;
                        (r.Content as Panel).Background = r.Background;
                        this.dm.MainDocumentPane.Items.Add(r);
                    }
                };
                newContent.Items.Add(panel);
                MenuItem stackPanel = new MenuItem { Header = "StackPanel" };
                stackPanel.Click += (s, e) =>
                {
                    NameDialog d = new NameDialog();
                    bool? ret = d.ShowDialog();
                    if (ret == true)
                    {
                        if (string.IsNullOrEmpty(d.TargetName) || string.IsNullOrEmpty(d.TargetTitle))
                        {
                            MessageBox.Show("名字和标题不能为空！");
                            return;
                        }

                        foreach (var item in this.dm.DockableContents)
                        {
                            if (item.Name.Equals(d.TargetName))
                            {
                                MessageBox.Show("已经存在相同名字对象");
                                return;
                            }
                        }
                        RegionController rc = ServiceLocator.Current.GetInstance<RegionController>();
                        if (rc.Regions.ContainsKey(d.TargetName))
                        {
                            MessageBoxResult result = MessageBox.Show("在本地目录中已经存在相同名字对象", "添加", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
                            if (result == MessageBoxResult.No)
                                return;
                        }
                        RegionContent r = new RegionContent(d.TargetName);
                        r.Title = d.TargetTitle;
                        r.Content = new DesignerStackPanel();
                        r.Background = Brushes.White;
                        (r.Content as Panel).Background = r.Background;
                        this.dm.MainDocumentPane.Items.Add(r);
                    }
                };
                newContent.Items.Add(stackPanel);


                RegionController rc1 = ServiceLocator.Current.GetInstance<RegionController>();
                foreach (var item in rc1.Regions)
                {
                    MenuItem mi1 = new MenuItem { Header = item.Key, Tag = item.Key };
                    mi1.Click += (s, ee) =>
                    {
                        RegionContent r = new RegionContent((s as MenuItem).Tag.ToString());
                        r.Load();
                        this.dm.MainDocumentPane.Items.Add(r);
                    };
                    addContent.Items.Add(mi1);
                }

                save.Click += (s, e) =>
                {
                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Indent = true;
                    settings.OmitXmlDeclaration = true;

                    using (XmlWriter w = XmlWriter.Create(UISetting.LayoutPath, settings))
                    {
                        this.dm.SaveLayout(w);
                    }
                };
                load.Click += (s, e) =>
                {
                    if (File.Exists(UISetting.LayoutPath))
                    {
                        using (XmlReader r = XmlReader.Create(UISetting.LayoutPath))
                        {
                            this.dm.RestoreLayout(r);
                        }
                    }
                };
                miM.Click += (s, e) =>
                {
                    bool isIn = bool.Parse((s as MenuItem).Tag.ToString());
                    DesignerContext.IsInDesignMode = !isIn;
                };
                #endregion
            }
            else
            {
                bool isin = DesignerContext.IsInDesignMode;
                MenuItem miM = new MenuItem { Header = isin == true ? "OutDesigner" : "InDesigner", Tag = isin };
                mi.Items.Add(miM);
                miM.Click += (s, e) =>
                {
                    bool isIn = bool.Parse((s as MenuItem).Tag.ToString());
                    DesignerContext.IsInDesignMode = !isIn;
                };
                list.Add(mi);
            }
            return list;
        }

        public void RestoreLayout()
        {
            if (File.Exists(UISetting.LayoutPath))
                this.dm.RestoreLayout(UISetting.LayoutPath);
        }
    }
}
